package sealte.hvpms.servlet.splitpage;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.hvpms.dao.UserDAO;
import sealte.hvpms.dao.impl.UserDAOImpl;
import sealte.hvpms.pojo.Admin;

import com.google.gson.Gson;

public class AdminBatchDeleteUC extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html");
		response.setHeader("Cache-Control","no-cache");	
		Admin admin = (Admin)request.getSession().getAttribute("admin");
		PrintWriter out = response.getWriter();
		if(admin!=null){
			String ucs = (String)request.getParameter("ucs");
			if(ucs!=null&&!ucs.equals("")){
				String user_path = null;
				File file = null;
				String[] fileNames = null;
				File fileIn = null;
				String[] uc_idStrs = ucs.split("[,]");
				int[] uc_ids = new int[uc_idStrs.length];
				for(int i=0;i<uc_idStrs.length;i++){
					uc_ids[i] = Integer.parseInt(uc_idStrs[i]);
					user_path = this.getServletContext().getRealPath("") + "\\upload_Images"+"\\"+String.valueOf(uc_ids[i]);
					file = new File(user_path);
					if(file.exists()){
						fileNames = file.list();
						for(int j=0;j<fileNames.length;j++){
							fileIn = new File(user_path+"/"+fileNames[j]);
							fileIn.delete();
						}
						file.delete();
					}
				}
				String message = "";
				UserDAO ub = new UserDAOImpl();
				boolean flag = ub.batchDeleteByID(uc_ids);
				if(flag){
					message = "ok";
				}else{
					message = "fail";
				}
				Gson gson = new Gson();
				String json = gson.toJson(message);
				response.getWriter().write(json);
			}else{
				out.println("系统错误！");
			}				
		}else{
			out.println("谢绝跨路径访问！");
		}
		
	}

}
